import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Simulator {
    static class SendThread extends Thread{
//        发送一条数据后的间隔时间 , 总共 100836 条 数据， 如果是模拟在 60秒内发完，那休息时间大概是60*1000/100836=0.59
        long time = 1;

        String path = "../data/ratings_sort_timestamp.csv";
        public SendThread(Socket client) {
            this.client = client;
        }

        Socket client ;
        @Override
        public void run() {
            System.out.println("开始发送数据..");
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                String line = null;
                while((line=reader.readLine())!=null)
                {
                    writer.write(line);
                    writer.write("\n");
                    writer.flush();
                    Thread.sleep(3);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e)
            {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发送数据完成..");

        }
    }
    public static void main(String[] args) throws IOException {

        ServerSocket server = new ServerSocket(8888);
        System.out.println("等待客户端连接..");
        while(true)
        {
            Socket client= server.accept();
            new SendThread(client).start();

        }
    }
}
